package com.lucky.back.service.impl;

import com.lucky.back.ex.ServiceException;
import com.lucky.back.mapper.ProductClassMapper;
import com.lucky.back.mapper.ProductMapper;
import com.lucky.back.pojo.dto.ProductAddNewDTO;
import com.lucky.back.pojo.entity.Product;
import com.lucky.back.pojo.vo.ProductClassStandardVO;
import com.lucky.back.pojo.vo.ProductListItemVO;
import com.lucky.back.pojo.vo.ProductStandardVO;
import com.lucky.back.service.IProductService;
import com.lucky.back.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ProductServiceImpl implements IProductService {

    @Autowired
    ProductMapper mapper;

    @Autowired
    ProductClassMapper classMapper;

    @Override
    public void updateById(Product product) {
        log.debug("开始处理【根据id修改商品详情】的业务，参数：{}", product);
        mapper.updateById(product);
    }

    @Override
    public ProductStandardVO getStandardById(Long id) {

        log.debug("开始处理【根据id查询商品详情】的业务，参数：{}", id);
        ProductStandardVO productStandardVO = mapper.getStandardById(id);
        if (productStandardVO == null) {
            String message = "获取商品详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return productStandardVO;
    }

    @Override
    public void addNew(ProductAddNewDTO productAddNewDTO) {
        int count = mapper.countByName(productAddNewDTO.getName());
        if (count != 0){
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加失败,名称已被占用");
        }
        Product product = new Product();
        BeanUtils.copyProperties(productAddNewDTO,product);
        System.out.println("即将执行插入商品service");
        System.out.println(product);
        mapper.insert(product);
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理删除请求");
        ProductStandardVO queruResult = mapper.getStandardById(id);
        if (queruResult == null){
            String message = "删除商品失败,,尝试访问数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("即将删除商品数据");
        mapper.deleteById(id);
        log.debug("删除商品，完成！");
    }

    @Override
    public List<ProductListItemVO> list() {
        log.debug("查询商品列表服务端");
        System.out.println("Service-productList----------");
        return mapper.list();
    }

    @Override
    public List<ProductListItemVO> listByClassId(Long id) {
        ProductClassStandardVO c = classMapper.getStandardById(id);
        return mapper.listByClass(c.getClass1());
    }

    @Override
    public List<ProductListItemVO> listByProduct(String product) {
        return mapper.listByProduct(product);
    }

    @Override
    public List<ProductListItemVO> listByPrice(){
        return mapper.listByPrice();
    }

    @Override
    public List<ProductListItemVO> listBySaleCount() {
        return mapper.listBySaleCount();
    }

}
